//
//  SimpleWebViewController.swift
//  yc-common
//
//  Created by 董鹏 on 2022/1/19.
//

import UIKit
import WebKit
import Toast_Swift
import SVProgressHUD

class SimpleWebViewController: RViewController {

    var url:String = "www.apple.com"
    var webView = WKWebView()
    var shareURL = URL.init(string: "")
    var isShare = false

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        webView = WKWebView(frame: self.view.frame)
        
        // 分享
        if isShare == true {
            let request = URLRequest(url: self.shareURL!)
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "はい", style: .done, target: self, action: #selector(share))
            webView.load(request)
        }else {
            // 网页
            let request = URLRequest(url: URL.init(string: self.url)!)
            webView.load(request)
        }
        
        webView.uiDelegate = self
        webView.navigationDelegate = self
        
        self.view.addSubview(webView)
        
        SVProgressHUD.show()
    }
    
    @objc func share() {
        shareFile(url: self.shareURL!)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        SVProgressHUD.dismiss()
    }
    
}

extension SimpleWebViewController: WKNavigationDelegate, WKUIDelegate {
        
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        SVProgressHUD.dismiss()
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        SVProgressHUD.dismiss()
    }
    
}

